package com.aiapphub.BaseCode.controller;

import com.aiapphub.BaseCode.common.ResponseResult;
import com.aiapphub.BaseCode.entity.Menu;
import com.aiapphub.BaseCode.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理控制器
 */
@RestController
@RequestMapping("/admin/acl/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单列表（树形结构）
     */
    @GetMapping("/list")
    public ResponseResult<List<Menu>> getMenuList() {
        List<Menu> menuList = menuService.getMenuTree();
        return ResponseResult.okResult(menuList);
    }

    /**
     * 根据ID获取菜单信息
     */
    @GetMapping("/get/{menuId}")
    public ResponseResult<Menu> getMenuById(@PathVariable Long menuId) {
        Menu menu = menuService.getById(menuId);
        return ResponseResult.okResult(menu);
    }

    /**
     * 新增菜单
     */
    @PostMapping("/save")
    public ResponseResult<Boolean> saveMenu(@RequestBody Menu menu) {
        boolean result = menuService.saveMenu(menu);
        return result ? ResponseResult.okResult(result) : ResponseResult.errorResult(500, "保存失败");
    }

    /**
     * 更新菜单
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> updateMenu(@RequestBody Menu menu) {
        boolean result = menuService.updateMenu(menu);
        return result ? ResponseResult.okResult(result) : ResponseResult.errorResult(500, "更新失败");
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/remove/{menuId}")
    public ResponseResult<Boolean> removeMenu(@PathVariable Long menuId) {
        boolean result = menuService.removeMenu(menuId);
        return result ? ResponseResult.okResult(result) : ResponseResult.errorResult(500, "删除失败");
    }

    /**
     * 检查菜单是否有子菜单
     */
    @GetMapping("/hasChildren/{menuId}")
    public ResponseResult<Boolean> hasChildren(@PathVariable Long menuId) {
        boolean hasChildren = menuService.hasChildren(menuId);
        return ResponseResult.okResult(hasChildren);
    }

    /**
     * 获取用户菜单列表
     */
    @GetMapping("/userMenu")
    public ResponseResult<List<Menu>> getUserMenus() {
        // 这里应该从上下文或token中获取当前用户ID
        // 暂时使用固定值1代替
        Long userId = 1L;
        List<Menu> userMenus = menuService.getUserMenus(userId);
        return ResponseResult.okResult(userMenus);
    }
}